// pages/recruit/jobList/jobList.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 基本参数
    select_text: "全部",
    status: '',
    page: 1,

    //招聘信息
    recruit_info: [],
    is_perfect: '',

    //submit 封装 Start
    area_name: '',
    plat_id: '',
    anchor_type: '',
    pay_low: '',
    pay_high: '',
    salary_type: '',
    keep_pay: '',
    page: 1,
    role: app.register_info.register_role,
    //submit 封装 End

    // modalShow
    select_flag: true,
    flag_id: true,

  },
  //加载
  load() {
    // console.log("加载")
    let that = this;
    let page = this.data.page;
    page = page + 1;
    that.setData({
      page: page
    })
    this.list();
    console.log("update")
  },
  // 加载
  list: function(e) {
    let that = this;
    let page = this.data.page
    that.get_jobList(that.data.status, page);
    wx.showLoading({
      icon: 'none',
    })
  },
  //获取列表
  get_jobList: function(_status, page) {
    let that = this;
    // that.setData({
    //   recruit_info:[]
    // })
    wx.request({
      url: app.globalData.url + 'api/guild/position/list',
      data: {
        token: app.globalData.token,
        status: _status,
        page: this.data.page,
      },
      method: 'POST',
      success: function(e) {
        // console.log(e)
        //普通情况
        if (e.data.data.list.current_page > e.data.data.list.last_page) {
          console.log("到底了");
          wx.showToast({
            title: '到底啦~',
            icon: 'none',
          })
        } else {
          if (that.data.page == "1") {
            let recruit_info = e.data.data.list.data;
            recruit_info.forEach(function(value, index) {
              var expire_time = value.expire_time,
                now_time = Date.parse(new Date()) / 1000,
                time_h = Math.floor((expire_time - now_time) / 3600),
                time_m = Math.floor(((expire_time - now_time) / 60) % 60);
              value.time_h = time_h;
              value.time_m = time_m;
            })
            that.setData({
              recruit_info: recruit_info,
              recruit_all: e.data.data,
            })
          } else {
            // console.log("不不不不不");
            let recruit_info = that.data.recruit_info;
            let _recruit_info = e.data.data.list.data;
            _recruit_info.forEach(function(value, index) {
              var expire_time = value.expire_time,
                now_time = Date.parse(new Date()) / 1000,
                time_h = Math.floor((expire_time - now_time) / 3600),
                time_m = Math.floor(((expire_time - now_time) / 60) % 60);
              value.time_h = time_h;
              value.time_m = time_m;
            })

            recruit_info.push(..._recruit_info)
            console.log(recruit_info);
            that.setData({
              recruit_info: recruit_info,
              recruit_all: e.data.data,
            })
          }
        }
        wx.hideLoading();
        // that.setData({
        //   recruit_info: e.data.data.list.data,
        // });
      },
    })
  },
  //去状态详情
  go_detail(e) {
    console.log(e.currentTarget.dataset.deatil_id)
    console.log("status+" + e.currentTarget.dataset.status)
    let deatil_id = e.currentTarget.dataset.deatil_id;
    let status = e.currentTarget.dataset.status;
    let check_number_real = e.currentTarget.dataset.check_number_real;
    let share_number = e.currentTarget.dataset.share_number;
    let anchor_send_resume = e.currentTarget.dataset.anchor_send_resume;
    if (status == "2") {
      wx.showToast({
        title: '正在审核中~',
        icon: "none",
      })
    } else {
      wx.navigateTo({
        url: '../jobDetail/jobDetail?deatil_id=' + deatil_id + "&status=" + status + "&check_number_real=" + check_number_real +

          "&share_number=" + share_number + "&anchor_send_resume=" + anchor_send_resume,
      })
    }

  },
  //筛选
  go_filter(e) {
    // console.log(e.currentTarget.dataset.filter_type)
    let that = this;
    let filter_type = e.currentTarget.dataset.filter_type;
    that.setData({
      page: 1,
      recruit_info: [],
    })
    switch (filter_type) {
      case "全部":
        console.log("全部")
        that.setData({
          status: '',
          select_text: "全部",
        })
        that.get_jobList(that.data.status);
        break;
      case "在线职位":
        console.log("在线职位")
        that.setData({
          status: 1,
          select_text: "在线职位",
        })
        that.get_jobList(that.data.status);
        break;
      case "审核中":
        console.log("审核中")
        that.setData({
          status: 2,
          select_text: "审核中",
        })
        that.get_jobList(that.data.status);
        break;
      case "已关闭":
        console.log("已关闭")
        that.setData({
          status: 4,
          select_text: "已关闭",
        })
        that.get_jobList(that.data.status);
        break;
      case "审核失败":
        console.log("审核失败")
        that.setData({
          status: 3,
          select_text: "审核失败",
        })
        that.get_jobList(that.data.status);
        break;
      default:
        console.log("other");
    }
    that.setData({
      select_flag: true,
    })
  },
  //剩余发布次数
  residue_time() {
    wx.request({
      url: app.globalData.url + 'api/recruit/limit/number',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        let last_time = e.data.data.is_send;
        this.setData({
          last_time: last_time,
        })
      },
    })
  },
  confirm_post() {
    this.setData({
      flag_id: false,
    })
  },
  //发布新职位
  go_postJob() {
    this.setData({
      flag_id: true
    })
    this.residue_time();
    if (this.data.is_perfect == 1) {
      let send_count = parseInt(this.data.recruit_all.send_count),
        send_recruit_upper = parseInt(this.data.recruit_all.send_recruit_upper);
      if (send_recruit_upper >= send_count) {
        wx.showModal({
          content: '职位发布数量已用完,暂不能发布新的职位',
          showCancel: false,
          confirmText: '好的',
          confirmColor: '#d0a76c',
        })
      } else {
        wx.navigateTo({
          url: '../postJob/postJob',
        })
      }

    } else {
      wx.showModal({
        content: '请先完善公司信息',
        showCancel: true,
        cancelText: '取消',
        cancelColor: '#333',
        confirmText: '好的',
        confirmColor: '#d0a76c',
        success: function(e) {
          if (e.confirm) {
            wx.navigateTo({
              url: '../../mine/CompanyBasis/CompanyBasis?ProcessJudge=false',
            })
          }
        },
      })

    }

  },
  //判断公司信息是否完善
  iscompany() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/guild/company/add',
      data: {
        token: app.globalData.token,
        type: 2,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
        that.setData({
          is_perfect: e.data.data.company.is_perfect,
        })
      },
    })
  },



  // 点击选择时间
  click_select() {
    let that = this;
    that.setData({
      select_flag: false,
    })
  },

  // 关闭蒙版
  modal_hide() {
    this.setData({
      select_flag: true,
      flag_id: true,
    })
  },
  go_shengji(e) {
    let deatil_id = e.currentTarget.dataset.deatil_id;
    wx.navigateTo({
      url: '../../payment/postBar/index?recruit_id=' + deatil_id
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this.get_jobList(this.data.status);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    // this.list();
    this.list();
    setTimeout(() => {
      var expire_time = this.data.expire_time;
      if (expire_time > 0) {
        setInterval(() => {
          var now_time = Date.parse(new Date()) / 1000;
          var time_h = Math.floor((expire_time - now_time) / 3600),
            time_m = Math.ceil(((expire_time - now_time) / 60) % 60);
          this.setData({
            expire_time: expire_time,
            time_h: time_h,
            time_m: time_m
          })
        }, 60000)
      }
    }, 1000)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      page: 1,
    })
    this.get_jobList(this.data.status, this.data.page);
    this.iscompany();
    this.residue_time();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      recruit_info: [],
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})